package com.intretech.umsin.oss.config;

import com.intretech.umsin.oss.entity.FileType;
import com.intretech.umsin.oss.service.FileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @ClassName: OssServiceFactory
 * @Description:TODO()
 * @author: 李启联
 * @date: 2022-04-24 14:35
 * @Copyright: Copyright (c), 2022
 */
@Configuration
public class OssServiceFactory {

    @Value("${oss-type}")
    private String ossType;
    public String getOssType() {
        return ossType;
    }
    public void setOssType(String ossType) {
        this.ossType = ossType;
    }

    protected Map<String, FileService> ossServiceMap = new HashMap<>();

    public OssServiceFactory(List<FileService> fileServices){
        for (FileService ossService : fileServices) {
            ossServiceMap.put(ossService.fileType().toString(),ossService);
        }
    }

    /**
     * 创建具体实现者
     * @return
     */
    public FileService createFileService(){
        FileService fileService = null;
        FileType fileType = null;
        try {
            fileType = Enum.valueOf(FileType.class,getOssType().toUpperCase());
        } catch (Exception e) {
            fileType = FileType.FTP;
        }
        fileService = ossServiceMap.get(fileType.toString());
        return fileService;
    }


}